package com.ruoyi.project.system.goodsteam.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.system.goodsteam.domain.GoodsTeam;
import com.ruoyi.project.system.goodsteam.domain.GoodsTeamDto;
import com.ruoyi.project.system.goodsteam.mapper.GoodsTeamMapper;
import com.ruoyi.project.system.goodsteam.service.IGoodsTeamService;
import com.ruoyi.project.system.oederteamgoods.domain.OrderTeamGoods;
import com.ruoyi.project.system.oederteamgoods.service.IOrderTeamGoodsService;
import com.ruoyi.project.system.orderteamorder.domain.OrderTeamOrder;
import com.ruoyi.project.system.orderteamorder.service.IOrderTeamOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 拼团商品列表Service业务层处理
 * 
 * @author f_baisi
 * @date 2021-03-02
 */
@Service
public class GoodsTeamServiceImpl implements IGoodsTeamService 
{
    @Autowired
    private GoodsTeamMapper goodsTeamMapper;
    @Autowired
    private IGoodsTeamService goodsTeamService;
    @Autowired
    private IOrderTeamOrderService orderTeamOrderService;
    @Autowired
    private IOrderTeamGoodsService orderTeamGoodsService;

    /**
     * 查询拼团商品列表
     * 
     * @param id 拼团商品列表ID
     * @return 拼团商品列表
     */
    @Override
    public GoodsTeam selectGoodsTeamById(String id)
    {
        return goodsTeamMapper.selectGoodsTeamById(id);
    }

    /**
     * 查询拼团商品列表列表
     * 
     * @param goodsTeam 拼团商品列表
     * @return 拼团商品列表
     */
    @Override
    public List<GoodsTeam> selectGoodsTeamList(GoodsTeam goodsTeam)
    {
        return goodsTeamMapper.selectGoodsTeamList(goodsTeam);
    }

    /**
     * 新增拼团商品列表
     * 
     * @param goodsTeam 拼团商品列表
     * @return 结果
     */
    @Override
    public int insertGoodsTeam(GoodsTeam goodsTeam)
    {
        return goodsTeamMapper.insertGoodsTeam(goodsTeam);
    }

    /**
     * 修改拼团商品列表
     * 
     * @param goodsTeam 拼团商品列表
     * @return 结果
     */
    @Override
    public int updateGoodsTeam(GoodsTeam goodsTeam)
    {
        return goodsTeamMapper.updateGoodsTeam(goodsTeam);
    }

    /**
     * 删除拼团商品列表对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteGoodsTeamByIds(String ids)
    {
        return goodsTeamMapper.deleteGoodsTeamByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除拼团商品列表信息
     * 
     * @param id 拼团商品列表ID
     * @return 结果
     */
    @Override
    public int deleteGoodsTeamById(String id)
    {
        return goodsTeamMapper.deleteGoodsTeamById(id);
    }

    @Override
    public GoodsTeamDto findGoodsTeamDtoByPId(String product) {
        return goodsTeamMapper.findGoodsTeamDtoByPId(product);
    }

    @Override
    public List<GoodsTeam> findAll() {
        return goodsTeamMapper.findAll();
    }

    @Override
    public void updateOrderTeamOrderStatus() {
        // 查询拼团商品
        List<GoodsTeam> goodsTeamAll = this.goodsTeamService.findAll();
        for (GoodsTeam goodsTeam:goodsTeamAll) {
            // 判断拼团商品时间是否过期
            if (goodsTeam.getGroupEnd().getTime()< DateUtils.getNowDate().getTime()) {
                // 判断拼团商品是否已下架
                if (goodsTeam.getStatus() != 1) {
                    // 下架过期拼团商品
                    GoodsTeam team = new GoodsTeam();
                    team.setId(goodsTeam.getId());
                    team.setStatus(1);
                    this.goodsTeamService.updateGoodsTeam(team);
                }

                OrderTeamGoods orderTeamGoods = new OrderTeamGoods();
                orderTeamGoods.setGoodsId(goodsTeam.getId());
                // 根据过期的商品ID查询拼团订单商品
                List<OrderTeamGoods> orderTeamGoodsList = this.orderTeamGoodsService.selectOrderTeamGoodsList(orderTeamGoods);
                for (OrderTeamGoods orderTeamG:orderTeamGoodsList) {
                    // 根据拼团订单商品查询拼团订单
                    OrderTeamOrder orderTeamOrder = this.orderTeamOrderService.selectOrderTeamOrderById(orderTeamG.getOrderId());
                    // 判断拼团订单状态是否是拼团中或是待付款
                    if (orderTeamOrder.getStatus() == 9 || orderTeamOrder.getStatus() == 0) {
                        OrderTeamOrder order = new OrderTeamOrder();
                        order.setId(orderTeamOrder.getId());
                        order.setStatus(10);
                        // 改变拼团订单状态为拼团失败
                        this.orderTeamOrderService.updateOrderTeamOrder(order);
                    }
                }
            }
        }
    }

}
